﻿#include "WidgetLogView.h"
#include <QLayout>
#include <QScrollBar>
#include "ui_base_lib.h"
WidgetLogView::WidgetLogView(QWidget *parent) : Widget(parent)
{
    QString strBroser;
    strBroser = styleWidgetGlobal;
#ifdef SIZE_MODIFI
    strBroser = sizeModifi(styleWidgetGlobal);
#endif
     int styleStart = 0;
    while(1){
        int start =strBroser.indexOf("font-size:" , styleStart);
        if(start >= 0){
            int startOffset = 10;
            int end =strBroser.indexOf("px" ,start + startOffset);
            if(end >= 0){

                QString strNUm =  strBroser.mid(start + startOffset , end - start - startOffset);
                int num = strNUm.toInt();

                num = num * getScreenScale() * getResolutionScale();
                strBroser.replace(start + startOffset , strNUm.size() , QString::number(num));
                styleStart = end + 2;
            }else {
                break;
            }

        }else{
            break;
        }

    }

    m_QTextBrowser = new QTextBrowser(this);
    m_QTextBrowser->move(0,0);
    m_QTextBrowser->resize(460,430);
    m_QTextBrowser->setStyleSheet(strBroser);
    m_QTextBrowser->document()->setMaximumBlockCount(200);

    m_buttonClear = new PushButtonSystem(this);
    m_buttonClear->setObjectName("clear");
    PushButtonSystem::initButton(m_buttonClear,tr(""),390, 0 , 30 , 30 , styleLPushButtonGlobal);
    connect(m_buttonClear, SIGNAL(clicked()), this, SLOT(on_buttonClearClick()));

    m_QTextBrowser->verticalScrollBar()->setStyleSheet(styleLScrollBarGlobal);


    this->addResizeBaseObj(m_buttonClear);
    m_buttonClear->setAutoResize(UI_RESIZE_AUTO);

    this->setStyleSheet(styleWidgetGlobal);
    connect(Logger::getInstance() , &Logger::evt_printfLog , this,&WidgetLogView::on_printfLog);
}

void WidgetLogView::resizeEvent(QResizeEvent *event)
{
    Widget::resizeEvent(event);
m_QTextBrowser->resize(this->width(),this->height());
}

void WidgetLogView::on_printfLog(QString str, int type)
{
    switch (type) {
    case LOGGER_TYPE_NORMAL:
        m_QTextBrowser->append("<font color=\"#9C9C9C\">" + str + "</font> ");
        break;
    case LOGGER_TYPE_WARING:
        m_QTextBrowser->append("<font color=\"#FF0000\">" + str + "</font> ");
        break;
    case LOGGER_TYPE_SUCCESS:
        m_QTextBrowser->append("<font color=\"#00FF00\">" + str + "</font> ");
        break;
    case LOGGER_TYPE_WARING_DELETE:
    {
        QTextCursor txtcur= m_QTextBrowser->textCursor();

        txtcur.movePosition(QTextCursor::EndOfLine,QTextCursor::KeepAnchor);
        txtcur.movePosition(QTextCursor::StartOfLine,QTextCursor::KeepAnchor);
        txtcur.removeSelectedText();
        txtcur.setPosition(QTextCursor::PreviousBlock);
        m_QTextBrowser->append("<font color=\"#FF0000\">" + str + "</font> ");
        break;
    }
    default:
        break;
    }
}

void WidgetLogView::on_buttonClearClick()
{
    m_QTextBrowser->clear();
}
